package com.example.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.fragment.databinding.ActivityMainBinding;
import com.example.fragment.databinding.HomeBinding;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class HomeActivity extends AppCompatActivity {
    private HomeBinding binding;
    private List<BadHabits> badHabits;
    private HomeAdapter adapter;
    private int currentPos;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = HomeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // initDate() 初始化数据
        initData();
        // 初始化视图
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        adapter = new HomeAdapter(badHabits);
        adapter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 获取选中的数据，并更新选中位置的背景色
                HomeAdapter.ViewHolder viewHolder = (HomeAdapter.ViewHolder) view.getTag();
                currentPos = viewHolder.getAdapterPosition();
                adapter.setCurrentIndex(currentPos);
                // 将选中位置的数据传递给main
                final Intent intent = new Intent(HomeActivity.this,MainActivity.class);
                final  Bundle bundle = new Bundle();
                bundle.putSerializable("badHabits",badHabits.get(currentPos));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        binding.recyclerView.setAdapter(adapter);
    }

    private void initData() {
        badHabits = new ArrayList<>();
        for (int i = 0; i < 10; i++){
            BadHabits bad;
            if (i % 2 == 0){
                bad = new BadHabits("Crime #" + i, new Date().toString(),true);
            }else{
                bad = new BadHabits("Crime #" + i, new Date().toString(),false);
            }
            badHabits.add(bad);
        }
    }
}
